/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.StatService;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class VersionStatContent extends Content {
	private ServiceMessages cons;
	private BifangGWTServiceAsync service;
	private String serCode = null;
	private String version = null;
	private VerticalPanel p;
	private GwtContext context;
	private Grid table1 = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			StatService b = (StatService) res.getResponse();
			table1.setHTML(0, 1, b.getStatBeginTime());
			table1.setHTML(1, 1, String.valueOf(b.getTotalRequest()));
			table1.setHTML(2, 1, String.valueOf(b.getSuccessRequest()));
			table1.setHTML(3, 1, String.valueOf(b.getTotalRequest()
					- b.getSuccessRequest()));
			if (b.getTotalRequest() == 0) {
				table1.setHTML(4, 1, "N/A");
			} else {
				table1.setHTML(4, 1, String.valueOf(Double.valueOf(Double.valueOf(b
						.getSuccessRequest())
						/ b.getTotalRequest() * 100).intValue() ) + "%");
			}
			table1.setHTML(5, 1, String.valueOf(b.getMaxResponseTime() / 1000));
			table1.setHTML(6, 1, String.valueOf(b.getMinResponseTime() / 1000));
			if (b.getTotalRequest() == 0) {
				table1.setHTML(7, 1, "N/A");
			} else {
				table1.setHTML(7, 1, String.valueOf(Double.valueOf(b
						.getTotalResponseTime())
						/ ( b.getSuccessRequest() * 1000 )));
			}
		}
	};

	public VersionStatContent(ServiceMessages cons,
			BifangGWTServiceAsync service1, GwtContext context,
			String serCode1, String ver) {
		this.cons = cons;
		this.service = service1;
		this.version = ver;
		this.serCode = serCode1;
		this.context = context;

		p = new VerticalPanel();
		p.setWidth("100%");
		p.setBorderWidth(0);
		p.setSpacing(10);

		p.add(new HTML(cons.stat()));

		table1 = new Grid(8, 2);
		table1.setWidth("70%");
		ColumnFormatter cf = table1.getColumnFormatter();
		cf.setWidth(0, "20%");
		cf.setWidth(1, "60%");

		table1.setCellPadding(1);
		table1.setCellSpacing(1);
		table1.setBorderWidth(1);

		CellFormatter ff = table1.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		ff.setStyleName(5, 0, "tablecol");
		ff.setStyleName(6, 0, "tablecol");
		ff.setStyleName(7, 0, "tablecol");

		table1.setHTML(0, 0, cons.statBegin());
		table1.setHTML(1, 0, cons.totalRequest());
		table1.setHTML(2, 0, cons.successRequest());
		table1.setHTML(3, 0, cons.failRequest());
		table1.setHTML(4, 0, cons.successRatio());
		table1.setHTML(5, 0, cons.maxResTime());
		table1.setHTML(6, 0, cons.minResTime());
		table1.setHTML(7, 0, cons.avgResTime());

		p.add(table1);

		Button b = new Button(cons.refresh());
		b.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				RequestObject req = new RequestObject();
				java.util.ArrayList<String> r = new java.util.ArrayList<String>();
				r.add(serCode);
				r.add(version);
				req.setRequest(r);
				req
						.setAction("han.bifang.gwt.server.action.service.GetVersionStatAction");

				service.call(req, cb);

			}
		});
		p.add(b);

		initWidget(p);
	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		java.util.ArrayList<String> r = new java.util.ArrayList<String>();
		r.add(serCode);
		r.add(version);
		req.setRequest(r);
		req
				.setAction("han.bifang.gwt.server.action.service.GetVersionStatAction");

		service.call(req, cb);

	}

}
